﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Data;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Imaging;
using System.Windows.Shapes;
//using System.Windows.Forms;
using StateGrid.User;

namespace StateGrid
{
    /// <summary>
    /// WelcomeWindow.xaml 的交互逻辑
    /// </summary>
    public partial class WelcomeWindow : Window
    {
        private UserManageDAO userDao = new UserManageDAO();

        public WelcomeWindow()
        {
            InitializeComponent();
            userDao = new UserManageDAO();
            if (userDao.isSqlValid == false)
            {
                MessageBox.Show("数据库联接错误，程序即将关闭", "错误", MessageBoxButton.OK, MessageBoxImage.Error);
                System.Environment.Exit(0);
            }
            txtUserName.Focus();
        }

        private void btnLogin_Click(object sender, RoutedEventArgs e)
        {

            if ("".Equals(this.txtUserName.Text.Trim()))
            {
                popEmptyUsername.IsOpen = true;
                return;
            }
            string password = null;
            try
            {
                IntPtr p = System.Runtime.InteropServices.Marshal.SecureStringToBSTR(this.txtPassword.SecurePassword);
                password = System.Runtime.InteropServices.Marshal.PtrToStringBSTR(p);
            }
            catch (Exception excpt)
            {
                MessageBox.Show(excpt.Message, "读取密码错误，程序即将关闭", MessageBoxButton.OK, MessageBoxImage.Error);
                System.Environment.Exit(0);
            }
            if ("".Equals(password))
            {
                popEmptyPassword.IsOpen = true;
                return;
            }

            User.User user = null;
            try
            {
                user = userDao.GetUserByName(this.txtUserName.Text.Trim());
            }
            catch (Exception ec)
            {
                MessageBox.Show(ec.Message, "数据库联接错误，程序即将关闭", MessageBoxButton.OK, MessageBoxImage.Error);
                System.Environment.Exit(0);
            }
            if (null == user)
            {
                popWrongUsername.IsOpen = true;
                return;
            }

            if (password.Equals(user.password))
            {
                Window w = new EditWindow(user);
                w.Show();
                this.Close();
            }
            else
            {
                popWrongPassword.IsOpen = true;
                return;
            }
        }

        private void btnReSet_Click(object sender, RoutedEventArgs e)
        {
            txtUserName.Clear();
            txtPassword.Clear();
        }

        private void txtUserName_KeyDown(object sender, KeyEventArgs e)
        {
            if (e.Key == Key.Enter)
            {
                btnLogin_Click(sender, e);
            }
        }

        private void txtPassword_KeyDown(object sender, KeyEventArgs e)
        {
            if (e.Key == Key.Enter)
            {
                btnLogin_Click(sender, e);
            }
        }
    }
}
